package com.google.ideas.MobileMeme;

import com.google.ideas.MobileMeme.stream.DummyContent;

// NOTE: this android.R is only here for dumb, built-in placeholder objects that we haven't replaced
// with real stuff yet.

import android.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * StreamListManager is the user-interface manager for the list of available streams. It talks to
 * the respective stream interfaces and gives them the appropriate names and statistics.
 * 
 * Well, it will when those systems exist.  In the mean time, it's just a helper class for StreamListActivity.
 */
public class StreamListManager implements OnItemClickListener {

  private static final String STATE_ACTIVATED_POSITION = "activated_position";
  private static final String STREAM_LIST_MANAGER = "StreamListManager";
  private ListView list_;
  private Callbacks mCallbacks = sDummyCallbacks;
  private int mActivatedPosition = ListView.INVALID_POSITION;

  public interface Callbacks {
    public void onItemSelected(String id);
  }

  private static Callbacks sDummyCallbacks = new Callbacks() {
    @Override
    public void onItemSelected(String id) {}
  };

  public StreamListManager() {}

  public void onViewCreated(Bundle savedInstanceState) {
    if (savedInstanceState != null 
        && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
      setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
    }
  }

  public void onSaveInstanceState(Bundle outState) {
    if (mActivatedPosition != ListView.INVALID_POSITION) {
      outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
    }
  }

  public void setActivateOnItemClick(boolean activateOnItemClick) {
    list_.setChoiceMode(
        activateOnItemClick ? 
            ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE);
  }

  public void setActivatedPosition(int position) {
    if (position == ListView.INVALID_POSITION) {
      list_.setItemChecked(mActivatedPosition, false);
    } else {
      list_.setItemChecked(position, true);
    }

    mActivatedPosition = position;
  }

  /**
   * @param list
   */
  public void ManageList(ListView list, Context context, Callbacks listener) {
    // TODO(lally): Auto-generated method stub
    list_ = list;
    list_.setAdapter(new ArrayAdapter<DummyContent.DummyItem>(
          context, R.layout.simple_list_item_activated_1, R.id.text1, 
          DummyContent.ITEMS));
    Log.d(STREAM_LIST_MANAGER, "Initializing the StreamListManager.");
    list_.setOnItemClickListener(this);
    mCallbacks = listener;
  }

  /* (non-Javadoc)
   * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
   */
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, 
      int position, long id) {
    Log.d(STREAM_LIST_MANAGER, "onItemClick(" + arg0 + ", " + arg1 + ", " 
         + position + ", " + id + ")");
    mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);    
  }
  
}
